//package com.example.getaudit.common.dmall;
//
//import com.alibaba.fastjson.JSON;
//import com.dmall.opendemo.util.HttpUtil;
//import com.dmall.opendemo.util.MyHttpRequest;
//import com.dmall.opendemo.util.SignUtil2;
//import com.dmall.sdk.ApiException;
//import org.junit.Test;
//
//import java.text.SimpleDateFormat;
//import java.util.Date;
//import java.util.HashMap;
//import java.util.Map;
//
///**
// * 请优先读resources/ReadMe
// * <p>
// * 使用自定义http请求开放平台，此类是调用实例
// * 分为3部分
// * 1、 从开放平台商户端查看 appkey,secret,code ,直接替换实例中用到的信息
// * 2、第一个方法 getAccessToken 是获取token，获取token有效期是1年
// * 3、第二个方法receiptAddTest 是根据获取的token调用业务接口API
// * 4、每三个方法 refreshToken 是当token过期后，进行token刷新可以获取到一个新token
// */
//public class SelfHttpRequestTest {
//
//    /**
//     * 自定义请求获取AccessToken
//     */
//    @Test
//    public void getAccessToken() {
//        String tokenServerUrl = "https://auth.dmall.com/auth/authToken/getTokenNew";
//        String appKey = "xxxxxx";
//        String code = "xxxxxx";
//        String appSecret = "xxxxxx";
//
//        String dtStr = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(new Date());
//        HashMap<String, String> map = new HashMap<String, String>();
//        map.put("appKey", appKey);
//        map.put("grant_type", "client_credentials");
//        map.put("code", code);
//        map.put("sign", SignUtil2.generateTokenSignature(appKey, code, dtStr, "HmacSHA256", appSecret, new HashMap<>()));
//        map.put("timestamp", dtStr);
//        String respStr = HttpUtil.httpPostRequest(tokenServerUrl, map);
//
//        System.out.println("respStr = " + respStr);
//    }
//
//
//    /**
//     * 业务接口调用  (结算收货单添加)
//     *
//     * @throws ApiException
//     */
//    @Test
//    public void receiptAddTest() {
//        String url = "https://open-api.dmall.com/cx/settlement/receipt/add";
//        String appKey = "xxxxxx";
//        String appSecret = "xxxxxx";
//        String accessToken = "xxxxxx";
//        Map<String, Object> treeMapP = new HashMap<>();
//        treeMapP.put("receiptList", "[{\"goodsCode\":\"\",\"goodsName\":\"测试承运商运费\",\"martGoodsCode\":\"184333\",\"martShopCode\":\"DC66\",\"orderDate\":1684415555130,\"orderItemNo\":10,\"orderNo\":\"CY2305166198104\",\"orderType\":\"E111\",\"quantity\":1,\"receiptDate\":1684415555130,\"receiptItemNo\":1,\"receiptNo\":\"2305166198104\",\"rowCount\":1,\"settleCompany\":\"C008777\",\"shopCode\":\"26\",\"shopName\":\"白天鹅\",\"source\":30,\"supplierCode\":\"10400102\",\"supplierName\":\"广州货运\",\"taxAmount\":1.200,\"taxCode\":\"JJ\",\"taxRate\":0.09,\"unit\":\"EA\",\"withouttaxAmount\":13.330,\"withouttaxPrice\":13.330,\"withtaxAmount\":14.530,\"withtaxPrice\":14.530}]");
//        treeMapP.put("mart","测试001");
//        String timeStamp = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(new Date());
//        String sign = SignUtil2.generateSignature(appKey, appSecret, accessToken, "HmacSHA256", timeStamp, treeMapP);
//
//        Map<String, String> headermap = new HashMap<String, String>();
//        headermap.put("app-key", appKey);
//        headermap.put("token", accessToken);
//        headermap.put("sign-method", "HmacSHA256");
//        headermap.put("timestamp", timeStamp);
//        headermap.put("sign", sign);
//        System.out.println("JSON.toJSONString(headermap) = " + JSON.toJSONString(headermap));
//        System.out.println("treeMapP = " + treeMapP);
//        try {
//            String dataStr = MyHttpRequest.sendRequest(url, appSecret, treeMapP, headermap);
//            System.out.println("dataStr = " + dataStr);
//        } catch (Exception ex) {
//            System.out.println("ex = " + ex);
//        }
//    }
//
//
//    /**
//     * 当token过期后，进行token刷新可以获取到一个新token
//     *
//     * @throws Exception
//     */
//    @Test
//    public void refreshToken() throws Exception {
//        String tokenServerUrl = "https://auth.dmall.com/oauth/refresh_token_new";
//        String appKey = "xxxxxx";
//        String code = "xxxxxx";
//        String appSecret = "xxxxxx";
//        String refreshToken = "xxxxxx";
//        String dtStr = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(new Date());
//        HashMap<String, Object> map = new HashMap<String, Object>();
//        map.put("client_id", appKey);
//        map.put("grant_type", "refresh_token");  //该参数与获取token时不同
//        map.put("client_secret", appSecret);
//        map.put("refresh_token", refreshToken);
//        map.put("code", code);
//        map.put("timestamp", dtStr);
//        String respStr = MyHttpRequest.sendRequest(tokenServerUrl, appSecret, map, new HashMap<>());
//        System.out.println("respStr = " + respStr);
//    }
//
//
//}
